package com.study.manage.dal;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.study.manage.domain.EvaluateGroupDetail;
import com.study.manage.domain.enumm.LiveDieState;

public interface EvaluateGroupDetailRepository
        extends PagingAndSortingRepository<EvaluateGroupDetail, Long> {

    List<EvaluateGroupDetail> findByGroupIdAndState(Long groupId, LiveDieState state);

    List<EvaluateGroupDetail> findByEvaluateIdAndState(Long evaluateId, LiveDieState state);

    @Query(nativeQuery = true,
            value = "SELECT sa_evaluate_group.title,sa_user_organiza.name FROM sa_evaluate_group_detail "
                    + "LEFT JOIN sa_evaluate_group ON sa_evaluate_group.id = sa_evaluate_group_detail.group_id "
                    + "LEFT JOIN sa_user_organiza ON sa_evaluate_group.user_organiza_id = sa_user_organiza.id "
                    + "WHERE sa_evaluate_group_detail.evaluate_id = ?1 AND sa_evaluate_group_detail.state = 0 ")
    Object getFinanceDatas(Long evaluateId);

    @Query("select t.evaluateId from EvaluateGroupDetail t,EvaluateGroup eg where eg.id=t.groupId and t.state=0 and eg.state in (1,2) and eg.userId=:userId")
    List<Long> findEvaluateIdByUserId(@Param("userId") Long userId);

    @Query("select t.evaluateId from EvaluateGroupDetail t,EvaluateGroup eg where eg.id=t.groupId and t.state=0 and eg.state in (1,2) and eg.id=:groupId")
    List<Long> findEvaluateIdByGroupId(@Param("groupId") Long groupId);
}
